package cn.huanba.service;

import cn.huanba.dao.DictDao;
import cn.huanba.entity.Dict;
import cn.huanba.service.mongo.MongoService;
import cn.huanba.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Dict)表服务实现类
 *
 * @author makejava
 * @since 2021-01-07 17:23:30
 */
@RestController
@RequestMapping("/dict")
public class DictServiceImpl {
    @Resource
    private DictDao dictDao;
    @Autowired
    private MongoService mongoService;
    @Autowired
    private OssUtils ossUtils;

    @RequestMapping("queryTypeAll")
    public List<Dict> queryTypeAll() {
        List<Dict> list=null;
        // 查询mongo
        Query query = Query.query(new Criteria());
        long count=mongoService.count(query, Dict.class);
        long count2=dictDao.getTypeCount();
        if(count<count2){
            mongoService.remove(query, Dict.class);
            list=dictDao.queryTypeAll();
            for (Dict dict : list) {
                mongoService.save(dict);
            }
        }else{
            list=mongoService.findList(query, Dict.class);
        }
        list.stream().forEach(e->e.setPicpath(ossUtils.createFileName(e.getPicpath())));
        return list;
    }
}